import 'dart:convert';
import 'dart:io';

import 'package:flutter_ahj_app/base/cunchu/UserInfoUtils.dart';
import 'package:flutter_ahj_app/base/finClip/ShareWx.dart';
import 'package:flutter_ahj_app/base/finClip/UserSync.dart';
import 'package:mop/mop.dart';

class FcSdk {
  static Future<void> init() async {
    // 1.创建Config
    FinStoreConfig storeConfig = Platform.isIOS ? new FinStoreConfig('0aviA7QQQPxKPoQkz9XI3IRkNulQSJKRefzEkOq95D4=', '3efc2ce19bddd599','https://api.finclip.com') : new FinStoreConfig('zegJOAgI1Bln+/8VD4b0g0UxI/lLKQO3hnLIYPpojAZlqGRPLmksJKRUXPEE/CfE', '4bceda527234b7c6','https://api.finclip.com');
    Config config = new Config([storeConfig]);

// 2.创建UIConfig
    UIConfig uiConfig = new UIConfig();
    uiConfig.isHideForwardMenu = true;
    uiConfig.isHideShareAppletMenu = true;


    final res = await Mop.instance.initSDK(config, uiConfig: uiConfig);
    // 初始化成功，才会去注册 回调接口
    if (res.containsKey("success") && res['success'] as bool) {
        Mop.instance.registerExtensionApi("userInfoSyncToApp",   UserSync.fcSyncToNavite );
        Mop.instance.registerExtensionApi("shareToWx",   ShareWx.doShare );


    }
  }


    /// 启动小程序， 如果有登录信息，会默认追加
  static openApplet(String appId, String path, String param) {
    // setup 1:获取当前token
    String token = UserInfoUtils.getUserToken();

    // setup 2: 得到json参数。 除了token，不排除以后添加新用户信息的可能，所以留个口子，备用  20230311
    // 如果带有中文，请添加 URI编码
    String query= "time="+DateTime.now().millisecondsSinceEpoch.toString();//防止参数前后一致，凡泰收不到
    query += '&userInfo='+json.encode(token != null ? UserSync.convertUserToken(token) : {});
    query += ("&param="+json.encode(param != null ? param : {}));

    // setup 3:执行启动
    RemoteAppletRequest request = new RemoteAppletRequest(apiServer: 'https://api.finclip.com', appletId: appId);
    request.startParams = {  'path':path, 'query':query };
    Mop.instance.startApplet(request);
  }
}
